<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="海关编码" prop="goods_code" label-width="100px">
        <el-input
          v-model="queryParams.goods_code"
          placeholder="请输入海关编码"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goods_name" label-width="130px">
        <el-input
          v-model="queryParams.goods_name"
          placeholder="请输入商品名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-action="'customsCode@importCustomscode'"
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="openImport"
        >海关编码导入
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list" style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column label="海关编码" align="center" prop="goods_code" width="100" />
      <el-table-column label="商品名称" align="center" prop="goods_name" width="300" :show-overflow-tooltip="true" />
      <el-table-column label="最惠国税率" align="center" prop="mfn_taxrate" />
      <el-table-column label="普通税率" align="center" prop="ordinary_taxrate" width="130" />
      <el-table-column label="增值税率" align="center" prop="increment_taxrate" />
      <el-table-column label="法定计量单位" align="center" prop="unit1">
        <template v-slot="scope">
          {{ getJson(scope.row.unit1,3) }}({{ scope.row.unit1 }})
        </template>
      </el-table-column>
      <el-table-column label="法定第二计量单位编码" align="center" prop="unit2" width="90">
        <template v-slot="scope">
          {{ getJson(scope.row.unit2,3) }}
          <span v-if="scope.row.unit2">({{ scope.row.unit2 }})</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template v-slot="scope">
          <el-button
            v-action="'customsCode@update'"
            size="mini"
            type="primary"
            @click="edit(scope.row)"
          >修改
          </el-button>
          <el-button
            v-action="'customsCode@delete'"
            size="mini"
            type="danger"
            @click="del(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />
    <!-- 修改 -->
    <Edit :open.sync="open" :form="form" @getList="getList" />
    <!-- 账册导入 -->
    <Import :export_open.sync="export_open" />
  </div>
</template>
<script>
import { codeList, codeEdit, codeDel } from '@/api/customscode'
import { permission } from '@/directive/permission/index.js' // 权限判断指令
import Import from './imports'
import Edit from './Edit'
export default {
  name: 'Index',
  directives: { permission },
  components: {
    Import,
    Edit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      export_loading: false,
      // 是否显示弹出层
      open: false,
      export_open: false,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      form: {},
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        goods_code: undefined,
        goods_name: undefined
      },
      ids: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      codeList(this.queryParams).then(response => {
        if (response.code === 10000) {
          that.list = response.data
          that.total = response.count
          that.loading = false
        }
      })
    },
    /** 修改 **/
    edit(row) {
      const that = this
      codeEdit({ id: row.id, type: 1 }).then(response => {
        if (response.code === 10000) {
          console.log(response.data)
          that.form = response.data
          that.open = true
        }
      })
    },
    /** 删除 */
    del(row) {
      const that = this
      that.$confirm('是否删除账册' + row.goods_code, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        codeDel({ id: row.id.toString() }).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        goods_code: undefined,
        goods_name: undefined
      }
      this.handleQuery()
    },
    // 打开导入弹窗
    openImport() {
      this.export_open = true
    },
    /** 选中的项 */
    handleSelectionChange(selection) {
      // this.ids = selection.map(item => item.orderId)
      // this.multiple = !selection.length
    }
  }
}
</script>
